package com.sherlocky.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 线程任务池，需要需要新开线程来处理任务，则使用这个帮助类即可
 * @author zhujj
 *
 */
public class ThreadPoolTaskUtil {
	private static Log logger = LogFactory.getLog(ThreadPoolTaskUtil.class);
	public static ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
	
	static{
		//如果池中的实际线程数小于1，无论是否其中有空闲的线程,都会给新的任务产生新的线程
		taskExecutor.setCorePoolSize(10);
		//如果提交的线程数大于corePoolSize并且小于maxPoolSize，只会创建corePoolSize的线程数被创建，然后将任务提交到队列中，直到队列慢为止
		taskExecutor.setMaxPoolSize(30);
		taskExecutor.initialize();
	}
	
	public static void addTask(Runnable task) {
		taskExecutor.execute(task);
	}
	
	public static void shutdown() {
		try{
			taskExecutor.shutdown();
		}catch(Exception ex) {
			logger.error("关闭ThreadPoolTaskUtil失败", ex);
		}
	}
}
